<?php

/**
 * 回款报表
 */
class ReturnedmoneyController extends Controller_Base
{
    /**
     *  回款报表- 列表
     */
    public function indexAction()
    {
        $getPage = $this->getRequest()->getQuery('page', 1);
        $post_date = $this->getRequest()->getQuery('post_date', date("Y-m-d", time()));
        $dataSelect = $this->getRequest()->getQuery('dataSelect', 'date');
        $mediationOrganId = trim($this->getRequest()->getQuery('mediationOrganId', 0));
        $assetsId = $this->getRequest()->getQuery('assetsId', 0);

        $page = intval(trim($getPage)) <= 0 ? 1 : intval(trim($getPage));
        $limit = 10;

        if ($dataSelect == 'date') {
            $begin_date_int = strtotime($post_date);
            $end_date_int = $begin_date_int + 86400;
        } elseif ($dataSelect == '周报' || $dataSelect == 'daterange') {
            $arr = explode(" 到", $post_date);
            $begin_date_int = strtotime($arr[0]);
            $end_date_int = strtotime($arr[1]) + 86400;
        } elseif ($dataSelect == 'month') {
            $begin_date_int = strtotime($post_date);
            $end_date_int = strtotime(date("Y-m", strtotime("+1 month", $begin_date_int)));
        } elseif ($dataSelect == 'year') {
            $begin_date_int = strtotime($post_date.'-01'.'-01');
            $end_date_int = strtotime(date("Y-1-1", strtotime("+1 year", $begin_date_int)));
        }



        // where
        $search = [];
        $where = $this->getWhere();

        array_push($where, "realRepaymentDate >= {$begin_date_int}  and realRepaymentDate<$end_date_int");

        array_push($where, " isDel=1  and status='finish_plan'");
        if ($assetsId) {
            array_push($where, "assetsId='{$assetsId}'");
        }

        //$fields = ["sum(realRepayAmount) as total_realRepayAmount , mediationOrganId, count( DISTINCT caseId) as caseNum"];
        $search['where'] = $where;

        $search['order'] = 'realRepaymentDate desc';
        $list = Module_Mediate_ReturnedMoneyPlanDetail::getInstance()->getAllBySearch([], $search);
        $countlist = $data = [];
        $tmp = [
            'mediatorId' => '',
            'mediatorName' => '',
            'mediationOrganId' => '',
            'caseIdArr' => [],
            'realRepayAmount' => 0,
        ];
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                if(empty($data[$v['mediatorId']])){
                    $data[$v['mediatorId']] = $tmp;
                }
                $data[$v['mediatorId']]['mediatorId'] = $v['mediatorId'];
                $data[$v['mediatorId']]['mediationOrganId'] = $v['mediationOrganId'];
                $data[$v['mediatorId']]['mediatorName'] = $v['mediatorName'];
                $data[$v['mediatorId']]['realRepayAmount'] = bcadd($data[$v['mediatorId']]['realRepayAmount'], $v['realRepayAmount'], 2);
                if (!in_array($v['caseId'], array_values($data[$v['mediatorId']]['caseIdArr']))) {
                    $data[$v['mediatorId']]['caseIdArr'][] = $v['caseId'];
                }
            }
            foreach ($data as $k => $v) {
                $countlist['returnMoney'] = bcadd($countlist['returnMoney'],$v['realRepayAmount'],2);
                $countlist['caseNum'] += count($v['caseIdArr']);
            }
        }

        $query['limit'] = $limit;
        $query['page'] = $page;
        $query['post_date'] = $post_date;
        $query['dataSelect'] = $dataSelect;
        $query['mediationOrganId'] = $mediationOrganId;
        $query['assetsId'] = $assetsId;

        //赋值调解部门
        $this->getMediationOrgan();
        //赋值资产方
        $this ->getAssets();
        //assign
        SmartyAdapter::assign('countlist', $countlist);
        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('page_query', empty($query) ? '' : http_build_query($query));
        SmartyAdapter::assign('query', $query);
        SmartyAdapter::assign('list', $data);
        SmartyAdapter::$templateName = 'returnedmoney/index';
    }


}
